// 内核主函数
// 功能：系统初始化、输出启动信息、进入主循环

#include "uart.h"
#include "ansi_escapes.h"
#include "printf.h"
#include "printf_test.h"
#include "../mm/kalloc.h"
#include "../mm/vm.h"   
#include "../mm/mm_test.h"
#include "../trap/trap.h"
#include "../trap/trap_test.h"

// 内核主函数（汇编中call跳转至此）
void main() {
    // //等待一段时间，展示清屏效果
    // for (int i = 0; i < 1000000000; i++)
    // {
    //     /* code */
    // }

    // // 输出启动成功信息
    // clear_screen();
    // cursor_home();

    // uart_puts("=== Kernel Boot Complete ===\n");
    // uart_puts("Hello, RISC-V Kernel!\n");
    // uart_puts("System is running...\n");

    // run_printf_tests();


    // // 程序结束后进入死循环
    // // 原因：1. 内核没有"退出"的概念；2. 防止CPU执行未知指令
    // while (1) {
    //     // 待添加相关处理
    // }

    // 初始化UART串口
    uart_init();
    
    // 初始化printf系统
    printf_init();
    
    // 初始化内存管理系统
    printf("Initializing memory management...\n");
    pmm_init();  // 初始化物理内存分配器
    printf("Physical memory allocator initialized\n");

    // 初始化中断处理系统
    printf("Initializing trap system...\n");
    trap_init();        // 初始化中断框架
    trap_init_hart();   // 初始化当前hart的中断
    timer_init();       // 初始化时钟
    printf("Trap system initialized\n");
    
    // 输出启动信息
    printf("\n");
    printf("========================================\n");
    printf("    RISC-V Operating System v1.0       \n");
    printf("========================================\n");
    printf("Hello OS!\n");
    printf("RISC-V Minimal Operating System Started.\n");
    printf("This is a kernel with printf support.\n");
    printf("\n");
    
    // 显示系统信息（使用printf格式化）
    printf("System Information:\n");
    printf("- Kernel loaded at: 0x%lx\n", 0x80000000UL);
    printf("- Stack pointer at: 0x%lx\n", 0x80010000UL);
    printf("- Printf system: %s\n", "Initialized");
    printf("- Console features: %s\n", "Enabled");
    printf("\n");
    
    // 基本printf功能演示
    printf("Basic Printf Demo:\n");
    printf("- Integer: %d\n", 42);
    printf("- Negative: %d\n", -123);
    printf("- Hex: 0x%x\n", 0xDEAD);
    printf("- Character: %c\n", 'A');
    printf("- String: %s\n", "Hello, Printf!");
    printf("- Pointer: %p\n", main);
    printf("\n");
    
    // 清屏功能演示
    printf("========================================\n");
    printf("      Clear Screen Demo Starting       \n");
    printf("========================================\n");
    printf("\n");
    printf("This is the content BEFORE clearing...\n");
    printf("Line 1: Hello World!\n");
    printf("Line 2: Testing clear screen function\n");
    printf("Line 3: You should see this content now\n");
    printf("Line 4: In 3 seconds, screen will be cleared\n");
    printf("\n");
    printf("Waiting 3 seconds...\n");
    
    // 延时模拟
    for (volatile int i = 0; i < 1000000000; i++) {
        // 延时循环
    }
    
    printf("Clearing screen NOW!\n");
    
    // 执行清屏
    clear_screen();
    
    run_printf_tests();
    
    // 运行内存管理测试
    printf("\n");
    printf("========================================\n");
    printf("    Running Memory Management Tests     \n");
    printf("========================================\n");
    run_all_mm_tests();
    
    // // 演示清屏功能
    // printf("\n");
    // printf("========================================\n");
    // printf("      Demo Clear Screen Function       \n");
    // printf("========================================\n");
    // demo_clear_screen();

    // 运行中断处理测试
    printf("\n");
    printf("========================================\n");
    printf("    Running Trap and Interrupt Tests    \n");
    printf("========================================\n");
    run_all_trap_tests();
    
    // 系统就绪提示
    printf("Available functions for testing:\n");
    printf("- printf() with various formats\n");
    printf("- console_clear() for screen clearing\n");
    printf("- console_set_color() for colors\n");
    printf("- run_printf_tests() for full test suite\n");
    printf("\n");
    printf("System ready for use!\n");
    
    // 系统主循环（防止退出）
    printf("Entering main loop...\n");
    while (1) {

    }
}
